package com.eastfair.venuebooking.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.base.BaseControllerInfo;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venuebooking.demand.BusinessOpportunityGroup;
import com.eastfair.venuebooking.dto.*;
import com.eastfair.venuebooking.entity.BusinessOpportunity;
import com.eastfair.venuebooking.service.BusinessOpportunityService;
import com.eastfair.venuebooking.service.ContactRefService;
import com.eastfair.venuebooking.vo.BusinessOpportunityVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 商机
 * </p>
 *
 * @author linan
 * @date 2022-06-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/businessOpportunity")
@Api(value = "BusinessOpportunity", tags = "商机")
@PreAuth(replace = "venuebooking:businessOpportunity:")
public class BusinessOpportunityController extends SuperController<BusinessOpportunityService, Long, BusinessOpportunity, BusinessOpportunityPageQuery, BusinessOpportunitySaveDTO, BusinessOpportunityUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private BusinessOpportunityService businessOpportunityService;

    @Resource
    private ContactRefService contactRefService;

    @Override
    public void handlerResult(IPage<BusinessOpportunity> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<BusinessOpportunity> businessOpportunityList = list.stream().map((map) -> {
            BusinessOpportunity businessOpportunity = BusinessOpportunity.builder().build();
            //TODO 请在这里完成转换
            return businessOpportunity;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(businessOpportunityList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<BusinessOpportunityPageQuery> params) {
        BusinessOpportunityPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 分页查询商机.
     *
     * @param pageParams the page params
     * @return the r
     */
    @PostMapping("/page/businessOpportunity")
    @BaseControllerInfo(voGroup = {BusinessOpportunityGroup.QueryBusinessOpportunity.class})
    public R<IPage<BusinessOpportunityVO>> queryBusinessOpportunity(
            @RequestBody PageParams<BusinessOpportunityQuery> pageParams) {
        log.info("pageBusinessOpportunity - 分页查询商机, pageParams={}", pageParams);
        return R.successDef(businessOpportunityService.listBusinessOpportunityVOs(pageParams));
    }

    /**
     * 分页查询我的商机.
     *
     * @param pageParams the page params
     * @return the r
     */
    @PostMapping("/page/me/businessOpportunity")
    @BaseControllerInfo(voGroup = {BusinessOpportunityGroup.QueryBusinessOpportunity.class})
    public R<IPage<BusinessOpportunityVO>> queryMeBusinessOpportunity(
            @RequestBody PageParams<BusinessOpportunityQuery> pageParams) {
        log.info("pageMeBusinessOpportunity - 分页查询我的商机, pageParams={}", pageParams);
        BusinessOpportunityQuery model = pageParams.getModel();
        model.setBelongTo(ContextUtil.getUserId());
        return R.successDef(businessOpportunityService.listBusinessOpportunityVOs(pageParams));
    }

    /**
     * 分页查询成交商机.
     *
     * @param pageParams the page params
     * @return the r
     */
    @PostMapping("/page/deal/businessOpportunity")
    @BaseControllerInfo(voGroup = {BusinessOpportunityGroup.QueryBusinessOpportunity.class})
    public R<IPage<BusinessOpportunityVO>> queryDealBusinessOpportunity(
            @RequestBody PageParams<BusinessOpportunityQuery> pageParams) {
        log.info("queryDealBusinessOpportunity - 分页查询成交商机, pageParams={}", pageParams);
        BusinessOpportunityQuery model = pageParams.getModel();
        model.setIsDeal(true);
        return R.successDef(businessOpportunityService.listBusinessOpportunityVOs(pageParams));
    }

    /**
     * 查询详情.
     *
     * @param id the id
     * @return the r
     */
    @GetMapping("/get/{id}")
    public R<BusinessOpportunityVO> getById(@PathVariable("id") Long id) {
        log.info("getById - 查询详情, id={}", id);
        return R.successDef(businessOpportunityService.getBusinessOpportunityVOById(id));
    }

    /**
     * 新增商机.
     *
     * @param saveDTO the save dto
     * @return the r
     */
    @SysLog("新增商机")
    @PostMapping("/save")
    public R<BusinessOpportunity> saveBusinessOpportunity(@RequestBody @Validated({BusinessOpportunityDTO.Save.class})
                                                      BusinessOpportunityDTO saveDTO) {
        log.info("saveBusinessOpportunity - 新增商机, saveDTO={}", saveDTO);
        return R.successDef(businessOpportunityService.saveBusinessOpportunity(saveDTO));
    }

    /**
     * 修改商机.
     *
     * @param updateDTO the save dto
     * @return the r
     */
    @SysLog("修改商机")
    @PostMapping("/update")
    public R<Boolean> updateBusinessOpportunity(
            @RequestBody @Validated({BusinessOpportunityDTO.Save.class, BusinessOpportunityDTO.Update.class})
                    BusinessOpportunityDTO updateDTO) {
        log.info("updateBusinessOpportunity - 修改商机, updateDTO={}", updateDTO);
        return R.successDef(businessOpportunityService.updateBusinessOpportunity(updateDTO));
    }

    /**
     * 删除商机.
     *
     * @param ids the ids
     * @return the r
     */
    @SysLog("删除商机")
    @PostMapping("/remove")
    public R<Boolean> deleteBusinessOpportunity(@RequestBody @NotEmpty(message = "商机id列表不可为空") List<Long> ids) {
        log.info("removeBusinessOpportunity - 删除商机, ids={}", ids);
        return R.successDef(businessOpportunityService.removeBusinessOpportunityByIds(ids));
    }

    /**
     * 分配商机.
     *
     * @param assignDTO the assign dto
     * @return the r
     */
    @SysLog("分配商机")
    @PostMapping("/assign")
    public R<Boolean> updateAssign(@RequestBody @Validated({BusinessOpportunityDTO.Assign.class})
                                           BusinessOpportunityAssignDTO assignDTO) {
        log.info("assign - 分配商机, assignDTO={}", assignDTO);

        return R.successDef(businessOpportunityService.updateBelongTo(assignDTO));
    }

    /**
     * 选择联系人.
     *
     * @param updateDTO the update dto
     * @return the r
     */
    @SysLog("选择联系人")
    @PostMapping("/choose/contact")
    public R<Boolean> updateChooseContact(@RequestBody @Validated(ContactRefDTO.Important.class) ContactRefDTO updateDTO) {
        log.info("chooseContact - 选择联系人, updateDTO={}", updateDTO);
        return R.successDef(contactRefService.updateChooseContactRef(updateDTO));
    }
}
